package co2.bean;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.BeanGetter;
import co2.engine.DAOInterface;
import co2.engine.SpringUtil;
import co2.pojos.Material;
import co2.pojos.MstUser;

@ManagedBean
@ViewScoped
public class FrontBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private List<Material> list;
	
	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	
	private Material material;
	
	@PostConstruct
	public void postCon(){
		material = new Material(); 
		try{
			/*Map map = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
			
			//ini untuk role sementara dihilangkan dulu
			if(map!=null && map.get("userId") != null){
				String userId = map.get("userId").toString();
				MstUser user = (MstUser) dao.get(MstUser.class, userId);
				list = dao.getAllMaterialByRole(user.getRole());
				
			}else{
				list = dao.getAllMaterialByRole("ALL");
				 
			}*/
			Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
			map.remove("materialFront");
			list = dao.getAllMaterial();
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
	
	public void signClick(){
		

		try {
			String extUrl=FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();

			FacesContext.getCurrentInstance().getExternalContext().redirect(extUrl+"/jsf/target/login_co2.jsf");
			 
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void detail(){
		Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
		map.remove("materialFromListContent");
		map.put("materialFront", material);
		try{
			String requestPath = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath() ;
			
			if(material!=null && material.getCategory() != null && material.getCategory().equals("XXX")){
				FacesContext.getCurrentInstance().getExternalContext().redirect(requestPath+"/jsf/target/keliling_lingkaran.jsf");
			}else if(material!=null && material.getCategory() != null && material.getCategory().equals("LKS")){
				FacesContext.getCurrentInstance().getExternalContext().redirect(requestPath+"/jsf/target/lks.jsf");
			}
			else{
				FacesContext.getCurrentInstance().getExternalContext().redirect(requestPath+"/jsf/target/detail_front.jsf");
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"redirect error",""));
		}
		
		
	}
	
	
	public List<Material> getList() {
		return list;
	}

	public void setList(List<Material> list) {
		this.list = list;
	}

	public Material getMaterial() {
		return material;
	}

	public void setMaterial(Material material) {
		this.material = material;
	}

	public DAOInterface getDao() {
		return dao;
	}

	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}
}
